package cn.scantd.core.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import cn.scantd.core.attrs.Scope;
import cn.scantd.core.util.EnumUtil;
import cn.scantd.kit.StrKit;

public class JavaType implements Serializable{

    private static final long serialVersionUID = -2484213103106395094L;
    
    private String name;
    private String fullName;
    private Scope scope;
    private JavaDoc javaDoc;
    private List<JavaField> fields = new ArrayList<JavaField>();
    private List<JavaMethod> methods = new ArrayList<JavaMethod>();
    private boolean isInterface;
    private boolean isAbstract;
    private String superClazz;
    private List<String> interfaces = new ArrayList<String>();
    
    public JavaType(TypeDeclaration typeDeclaration) {
        name = StrKit.trim(typeDeclaration.getName().getIdentifier());
        fullName = StrKit.trim(typeDeclaration.resolveBinding().getQualifiedName());
        scope = EnumUtil.getScope(typeDeclaration.getModifiers());
        javaDoc = new JavaDoc(typeDeclaration.getJavadoc());
        isInterface = typeDeclaration.isInterface();
        isAbstract = typeDeclaration.modifiers().contains(Modifier.ABSTRACT);
        if (typeDeclaration.getSuperclassType() != null) {
            superClazz = StrKit.trim(typeDeclaration.getSuperclassType().resolveBinding().getQualifiedName());
        }
        for (Object obj : typeDeclaration.superInterfaceTypes()) {
            if (obj instanceof SimpleType) {
                SimpleType interfaceType = (SimpleType) obj;
                interfaces.add(interfaceType.resolveBinding().getQualifiedName());
            } else if (obj instanceof ParameterizedType) {
                ParameterizedType interfaceType = (ParameterizedType) obj;
                interfaces.add(interfaceType.resolveBinding().getQualifiedName());
            } else {
                throw new RuntimeException("Error type");
            }
            
        }
    }
    
    public void addField(JavaField javaField) {
        fields.add(javaField);
    }
    
    public void addMethod(JavaMethod javaMethod) {
        methods.add(javaMethod);
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public Scope getScope() {
        return scope;
    }
    public void setScope(Scope scope) {
        this.scope = scope;
    }
    public JavaDoc getJavaDoc() {
        return javaDoc;
    }
    public void setJavaDoc(JavaDoc javaDoc) {
        this.javaDoc = javaDoc;
    }
    public List<JavaField> getFields() {
        return fields;
    }
    public void setFields(List<JavaField> fields) {
        this.fields = fields;
    }
    public List<JavaMethod> getMethods() {
        return methods;
    }
    public void setMethods(List<JavaMethod> methods) {
        this.methods = methods;
    }
    public boolean isInterface() {
        return isInterface;
    }
    public void setInterface(boolean isInterface) {
        this.isInterface = isInterface;
    }
    public boolean isAbstract() {
        return isAbstract;
    }
    public void setAbstract(boolean isAbstract) {
        this.isAbstract = isAbstract;
    }
    public String getSuperClazz() {
        return superClazz;
    }
    public void setSuperClazz(String superClazz) {
        this.superClazz = superClazz;
    }
    public List<String> getInterfaces() {
        return interfaces;
    }
    public void setInterfaces(List<String> interfaces) {
        this.interfaces = interfaces;
    }
}
